<template>
  <el-dialog @close="close" v-model="dialogVisible" :title="title" width="50%">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="100px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username" placeholder="请输入用户名" :disabled="ruleForm.id" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password" placeholder="请输入密码" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="ruleForm.phone"
          maxlength="11"
          placeholder="请输入手机号"
          onkeyup="this.value = this.value.replace(/[^\d]/g,'');"
        />
      </el-form-item>
      <el-form-item label="权限" prop="auth_ids">
        <el-checkbox-group v-model="ruleForm.auth_ids">
          <el-checkbox :label="item.id" v-for="item in authArr" :key="item.id">{{
            item.title
          }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="ruleForm.remark" type="textarea" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" :loading="saveLoading" @click="handleClose(ruleFormRef)"
          >确定</el-button
        >
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { ElMessageBox, ElMessage, FormInstance, ElNotification } from 'element-plus'
import { nextTick, reactive, ref } from 'vue'
import { authList, systemAdd, systemEdit } from '@/api/admin'
import { number } from 'echarts'
const authArr = ref()
const saveLoading = ref(false)
const ruleFormRef = ref<FormInstance>()
const dialogVisible = ref<boolean>(false)
const title = ref('新增用户')

const rules = reactive({
  username: [{ required: true, message: '请输入', trigger: 'blur' }],
  password: [
    { required: true, message: '请输入', trigger: 'blur' },
    { min: 6, message: '长度最少 6 位', trigger: 'blur' },
  ],
  auth_ids: [{ required: true, message: '请选择', trigger: 'change' }],
})

const ruleForm = reactive({
  username: '',
  password: '',
  phone: '',
  auth_ids: [],
  remark: '',
})

const emit = defineEmits<{ (event: 'refresh'): void }>()

function close() {
  rules.password[0].required = true
  ruleFormRef.value.resetFields()
  if (ruleForm.id) delete ruleForm.id
  Object.keys(ruleForm).forEach((key) => {
    if (key === 'auth_ids') ruleForm[key] = []
    else ruleForm[key] = ''
  })
}

const show = (item = {}) => {
  title.value = '新增用户'
  if (item.id) {
    title.value = '编辑用户'
    ruleForm.id = item.id
    ruleForm.username = item.username
    ruleForm.phone = item.phone
    ruleForm.auth_ids = item.auth_ids || []
    ruleForm.remark = item.remark
    if (item.auth_ids) {
      ruleForm.auth_ids = item.auth_ids.split(',')
      for (let i = 0; i < ruleForm.auth_ids.length; i++) {
        ruleForm.auth_ids[i] = Number(ruleForm.auth_ids[i])
      }
    } else {
      ruleForm.auth_ids = []
    }
    rules.password[0].required = false
  }
  dialogVisible.value = true
}

const handleClose = async (done: () => void) => {
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      saveLoading.value = true
      if (ruleForm.id) {
        //编辑
        systemEdit(ruleForm).then((res) => {
          if (res.code == 200) {
            ElNotification({
              title: res.data,
              message: res.msg,
              type: 'success',
            })
            emit('refresh')
            dialogVisible.value = false
          }
          saveLoading.value = false
        })
      } else {
        //新增
        systemAdd(ruleForm).then((res) => {
          if (res.code == 200) {
            ElNotification({
              title: res.data,
              message: res.msg,
              type: 'success',
            })
            emit('refresh')
            dialogVisible.value = false
          }
          saveLoading.value = false
        })
      }
    }
  })
}

const getAuthList = () => {
  authList({}).then((res) => {
    if (res.code == 200) {
      authArr.value = res.data
    }
  })
}
getAuthList()
defineExpose({
  show,
})
</script>
<style lang="scss" scoped>
</style>

